package com.tledu.springboot04.controlller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.springboot04.constants.CommonConstans;
import com.tledu.springboot04.core.result.AjaxResult;
import com.tledu.springboot04.domain.User;
import com.tledu.springboot04.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Antg
 * @date 2021/8/25  17:46
 */
@Controller
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    @GetMapping("/index")
    public String index() {
        return "user/user-list";
    }

    @GetMapping("/list")
    @ResponseBody
    public AjaxResult<PageInfo<User>> list(User user, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? CommonConstans.PAGESIZE : pageSize;

        PageHelper.startPage(pageNum, pageSize);
        List<User> temp = userService.list(user);
        PageInfo<User> list = PageInfo.of(temp);
        return new AjaxResult<>(list);
    }

    @GetMapping("/del")
    @ResponseBody
    public AjaxResult del(Integer id) {
        int result = userService.del(id);
        if (result >= 1) {
            return new AjaxResult("成功");
        } else {
            return new AjaxResult("失败", 500);
        }
    }
}
